package cn.eeepay.boss.action.wetchatPublic;

import cn.eeepay.boss.system.SystemLog;
import cn.eeepay.framework.db.pagination.Page;
import cn.eeepay.framework.model.wetchatPublic.PublicAccountMsgBatchEntity;
import cn.eeepay.framework.service.unTransactionalImpl.wetchatPublic.PublicAccountMsgBatchService;
import cn.eeepay.framework.util.ResultModel;
import com.alibaba.fastjson.JSON;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 公众号模板消息推送批次记录表
 *
 * @author quanhz
 * @email
 * @date 2020-07-29 15:31:47
 */
@RestController
@RequestMapping("/publicAccountMsgBatch")
public class PublicAccountMsgBatchController {

    private Logger log = LoggerFactory.getLogger(PublicAccountMsgBatchController.class);

    @Resource
    private PublicAccountMsgBatchService publicAccountMsgBatchService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public ResultModel list(
            @Param("page") Page<PublicAccountMsgBatchEntity> page, @RequestParam("info") String info) {

        try {
            publicAccountMsgBatchService.queryPage(
                    page, JSON.parseObject(info, PublicAccountMsgBatchEntity.class));
            return ResultModel.ok("查询公众号推送信息成功").put("page", page);
        } catch (Exception e) {
            log.error("查询公众号推送信息失败", e);
            return ResultModel.error("查询公众号推送信息失败");
        }
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public ResultModel info(@PathVariable("id") Long id) {
        try {
            return ResultModel.ok().put("data", publicAccountMsgBatchService.getById(id));
        } catch (Exception e) {
            log.error("获取推送记录失败", e);
            return ResultModel.error("获取推送记录失败");
        }
    }

    /**
     * 导入
     */
    @RequestMapping("/import")
    @SystemLog(description = "微信推送导入自定义用户", operCode = "publicAccountMsgBatch.import")
    public ResultModel importBatch(
            @RequestParam(value = "file", required = false) MultipartFile file, @RequestParam("info") String info) {
        try {
            if (!file.isEmpty()) {
                String format =
                        file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
                if (!format.equals(".xls") && !format.equals(".xlsx")) {
                    return ResultModel.error("导入文件格式错误");
                }
            }
            return publicAccountMsgBatchService.importBatch(
                    file, JSON.parseObject(info, PublicAccountMsgBatchEntity.class));
        } catch (Exception e) {
            log.error("导入公众号模板消息推送批次记录失败", e);
            return ResultModel.error("导入公众号模板消息推送批次记录失败");
        }
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @SystemLog(description = "保存微信推送", operCode = "publicAccountMsgBatch.save")
    public ResultModel save(@RequestBody PublicAccountMsgBatchEntity info) {
        try {
            return publicAccountMsgBatchService.save(info);
        } catch (Exception e) {
            log.error("保存公众号模板消息推送批次记录失败", e);
            return ResultModel.error("保存公众号模板消息推送批次记录失败");
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/updateTimeBlock")
    @SystemLog(description = "推送记录修改时间段", operCode = "publicAccountMsgBatch.updateTimeBlock")
    public ResultModel updateTimeBlock(@RequestBody PublicAccountMsgBatchEntity publicAccountMsgBatch) {

        try {
            publicAccountMsgBatchService.updateTimeBlock(publicAccountMsgBatch);
            return ResultModel.ok("修改成功");
        } catch (Exception e) {
            log.error("修改失败", e);
            return ResultModel.error("修改失败");
        }
    }

    /**
     * 终止
     */
    @RequestMapping("/stop")
    @SystemLog(description = "终止", operCode = "publicAccountMsgBatch.stop")
    public ResultModel stop(@RequestParam Long id) {
        try {
            return publicAccountMsgBatchService.stop(id);
        } catch (Exception e) {
            log.error("终止失败", e);
            return ResultModel.error("终止失败");
        }
    }

    /**
     * 继续发送
     */
    @RequestMapping("/start")
    @SystemLog(description = "继续发送", operCode = "publicAccountMsgBatch.start")
    public ResultModel start(@RequestParam Long id) {
        try {
            return publicAccountMsgBatchService.start(id);
        } catch (Exception e) {
            log.error("继续发送失败", e);
            return ResultModel.error("继续发送失败");
        }
    }

    /**
     * 暂停
     */
    @RequestMapping("/suspend")
    @SystemLog(description = "暂停", operCode = "publicAccountMsgBatch.suspend")
    public ResultModel suspend(@RequestParam Long id) {
        try {
            return publicAccountMsgBatchService.suspend(id);
        } catch (Exception e) {
            log.error("暂停失败", e);
            return ResultModel.error("暂停失败");
        }
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @SystemLog(description = "删除", operCode = "publicAccountMsgBatch.delete")
    public ResultModel delete(@RequestParam String pushBatchNo) {
        try {
            publicAccountMsgBatchService.removeByPushBatchNo(pushBatchNo);
            return ResultModel.ok("删除成功");
        } catch (Exception e) {
            log.error("删除失败", e);
            return ResultModel.error("删除失败");
        }
    }


    /**
     * 发送
     */
    @RequestMapping("/send/{pushBatchNo}")
    @SystemLog(description = "发送信息", operCode = "publicAccountMsgBatch.send")
    public ResultModel send(@PathVariable("pushBatchNo") String pushBatchNo) {
        try {
            return ResultModel.ok("发送成功").put("data", publicAccountMsgBatchService.send(pushBatchNo));
        } catch (Exception e) {
            log.error("发送失败", e);
            return ResultModel.error("发送失败");
        }
    }
}
